//
//  ExView.swift
//  swiftTXH
//
//  Created by Mac mini on 2020/6/9.
//  Copyright © 2020 Mac mini. All rights reserved.
//

import UIKit

public extension UIView {
    var cnm_width: CGFloat {
        get { return self.frame.size.width }
        set { self.frame.size.width = newValue }
    }
    
    var cnm_height: CGFloat {
        get { return self.frame.size.height }
        set { self.frame.size.height = newValue }
    }
    
    var cnm_x: CGFloat {
        get { return self.frame.origin.x }
        set { return self.frame.origin.x = newValue }
    }
    
    var cnm_y: CGFloat {
        get { return self.frame.origin.y }
        set { return self.frame.origin.y = newValue }
    }
    
    var cnm_size: CGSize {
        get { return self.frame.size }
        set { self.frame.size = newValue }
    }
    
    var cnm_maxX: CGFloat {
        return self.frame.maxX
    }
    
    var cnm_maxY: CGFloat {
        return self.frame.maxY
    }
}

public extension UIView {
    /// 设置`圆角`, `边框`, `边框颜色`(系统提供) 四个角
    func cnm_setSysCornerRadius(radius: CGFloat = 8.0, borderWidth: CGFloat = 1.0, borderColor: UIColor = .lightGray) {
        self.layer.cornerRadius = radius
        self.layer.borderWidth = borderWidth
        self.layer.borderColor = borderColor.cgColor
    }
    
    /// 设置`圆角` 四个角
    func cnm_setCornerRadius(radius: CGFloat, cornerColor: UIColor) {
        self._cnm_setCornerRadius(radius: radius, cornerColor: cornerColor, borderWidth: 0.0, borderColor: .clear)
    }
    
    /// 设置`圆角` + `边框` + `边框颜色` 四个角
    func cnm_setCornerRadius(radius: CGFloat, cornerColor: UIColor, borderWidth: CGFloat, borderColor: UIColor) {
        self._cnm_setCornerRadius(radius: radius, cornerColor: cornerColor, borderWidth: borderWidth, borderColor: borderColor)
    }
    
    /// 设置某个`圆角`
    func cnm_filletedCorner(_ cornerRadii: CGFloat, _ roundingCorners: UIRectCorner)  {
        DispatchQueue.main.async {
            let fieldPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: roundingCorners, cornerRadii: CGSize(width: cornerRadii, height: cornerRadii))
            let fieldLayer = CAShapeLayer()
            fieldLayer.frame = self.bounds
            fieldLayer.path = fieldPath.cgPath
            self.layer.mask = fieldLayer
        }
    }
    
    /// 设置某个 `圆角` 适合 iOS 11.0+
    @available(iOS 11.0, *)
    func cnm_filleted11Corner(_ radius: CGFloat, _ cornerMask: CACornerMask) {
        // iOS11:只需要带用这个系统方法就可以随意设置View的圆角了
        self.layer.cornerRadius = radius
        self.layer.maskedCorners = cornerMask
    }
    
    /// 设置`阴影` + `圆角` 并存
    func cnm_addCornerAndShadow(superview: UIView, conrners: UIRectCorner, radius: CGFloat = 3, shadowColor: UIColor, shadowOffset: CGSize = .zero, shadowOpacity: Float = 1, shadowRadius: CGFloat = 3) {
        DispatchQueue.main.async {
            let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
            let maskLayer = CAShapeLayer()
            maskLayer.frame = self.bounds
            maskLayer.path = maskPath.cgPath
            self.layer.mask = maskLayer
        
            let subLayer = CALayer()
            let fixframe = self.frame
            subLayer.frame = fixframe
            subLayer.cornerRadius = shadowRadius
            subLayer.backgroundColor = superview.backgroundColor?.cgColor
            // shadowColor阴影颜色
            subLayer.shadowColor = shadowColor.cgColor
            // shadowOffset阴影偏移,x向右偏移3，y向下偏移2，默认(0, -3),这个跟shadowRadius配合使用
            subLayer.shadowOffset = shadowOffset
            // 阴影透明度，默认0
            subLayer.shadowOpacity = shadowOpacity
            // 阴影半径，默认3
            subLayer.shadowRadius = shadowRadius
            superview.layer.insertSublayer(subLayer, below: self.layer)
        }
    }
}

public extension UIView {
    private func _cnm_setCornerRadius(radius: CGFloat, cornerColor: UIColor, borderWidth: CGFloat, borderColor: UIColor) {
        let imageView = UIImageView(image: self._cnm_drawRectWithCornerRadius(radius: radius, cornerColor: cornerColor, borderWidth: borderWidth, borderColor: borderColor))
        self.insertSubview(imageView, at: 2)
    }
    
    private func _cnm_drawRectWithCornerRadius(radius: CGFloat, cornerColor: UIColor, borderWidth: CGFloat, borderColor: UIColor) -> UIImage? {
        
        let size = self.bounds.size
        
        let halfBorderWidth = borderWidth * 0.5
        
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        
        // UIGraphicsGetCurrentContext: 设置视图的frame后才有cnt
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        
        context.setLineWidth(borderWidth)
        context.setStrokeColor(borderColor.cgColor)
        context.setFillColor(cornerColor.cgColor)
        
        // 开始坐标右边开始
        context.move(to: CGPoint(x: size.width - halfBorderWidth, y: radius + halfBorderWidth))
        // 右下角
        context.addArc(tangent1End: CGPoint(x: size.width - halfBorderWidth, y: size.height - halfBorderWidth), tangent2End: CGPoint(x: size.width - halfBorderWidth - radius, y: size.height - halfBorderWidth), radius: radius)
        // 左下角角度
        context.addArc(tangent1End: CGPoint(x: halfBorderWidth, y: size.height - halfBorderWidth), tangent2End: CGPoint(x: halfBorderWidth, y: size.height - radius - halfBorderWidth), radius: radius)
        // 左上角
        context.addArc(tangent1End: CGPoint(x: halfBorderWidth, y: halfBorderWidth), tangent2End: CGPoint(x: size.width - halfBorderWidth, y: halfBorderWidth), radius: radius)
        // 右上角
        context.addArc(tangent1End: CGPoint(x: size.width - halfBorderWidth, y: halfBorderWidth), tangent2End: CGPoint(x: size.width - halfBorderWidth, y: radius + halfBorderWidth), radius: radius)
        
        context.drawPath(using: .fillStroke)
        
        let outputImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return outputImage
    }
}
